
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>线锁机制 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>线锁机制 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">`synchronized`关键字可以通过哪两种方式来实现同步加锁？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`synchronized`可以通过两种方式实现：1. 方法级锁，即修饰整个方法。2. 代码块锁，即只对部分代码块加锁，这种方式效率更高。</div>
          </div>
          <div class="card-source">来源: 2. `synchronized`锁的实现</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">当`synchronized`分别修饰一个实例方法和一个静态方法时，锁定的对象是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">当`synchronized`修饰实例方法时，锁的对象是当前实例对象。当修饰静态方法时，锁的是该类的`Class`对象。</div>
          </div>
          <div class="card-source">来源: 3. 锁对象和锁的内部实现</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">请简述Java中`synchronized`锁的升级路径及其触发条件。</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">锁的升级路径为：偏向锁 -> 轻量级锁 -> 重量级锁。当没有线程争用时为偏向锁；当有其他线程尝试竞争时升级为轻量级锁；当多个线程持续争用锁时，最终会升级为重量级锁。</div>
          </div>
          <div class="card-source">来源: 4. 锁的升级机制：偏向锁、轻量级锁和重量级锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">`ReentrantLock`的内部实现依赖于哪个核心组件来管理线程？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`ReentrantLock`的内部实现依赖于一个`AbstractQueuedSynchronizer`（AQS）类，通过AQS来实现锁的队列管理和线程调度。</div>
          </div>
          <div class="card-source">来源: 5. `ReentrantLock`的实现</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">`ReentrantLock`支持哪两种公平性策略？如何启用公平锁？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`ReentrantLock`支持公平锁和非公平锁。通过在构造函数中传入`true`（例如 `new ReentrantLock(true)`）可以启用公平锁，它能保证等待时间最长的线程优先获取锁。</div>
          </div>
          <div class="card-source">来源: 6. 锁的公平性</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">`ReentrantLock`是如何实现可中断锁的？该机制有何优点？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`ReentrantLock`通过`lockInterruptibly()`方法实现可中断锁。优点是线程在等待锁的过程中可以响应中断请求，并抛出`InterruptedException`，从而避免因长时间等待锁而导致的阻塞。</div>
          </div>
          <div class="card-source">来源: 7. 锁的可中断性</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">JVM是如何在内部标识和管理一个对象的锁状态的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">JVM通过对象头中的“Mark Word”字段来标识对象的锁状态。`Mark Word`包含了对象的锁信息，例如是否已加锁、锁的类型（偏向锁、轻量级锁等）以及锁的持有者。</div>
          </div>
          <div class="card-source">来源: 3. 锁对象和锁的内部实现</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">偏向锁、轻量级锁和重量级锁分别适用于怎样的并发场景？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">偏向锁适用于没有其他线程争用锁的场景。轻量级锁适用于有其他线程竞争，但锁竞争不激烈的场景。重量级锁适用于有多个线程持续激烈争用锁的场景。</div>
          </div>
          <div class="card-source">来源: 4. 锁的升级机制：偏向锁、轻量级锁和重量级锁</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">相较于Java内置的`synchronized`锁，`ReentrantLock`提供了哪些更强大的功能？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`ReentrantLock`是显式锁，提供了比`synchronized`更多的控制能力，例如支持公平锁、可中断锁以及定时锁等特性。</div>
          </div>
          <div class="card-source">来源: 5. `ReentrantLock`的实现</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
